From: Siebrand Mazeland Date: Wed, 27 Nov 2013 10:17:06 +0000 (+0100) Subject: Remove underscore from classes LBFactory_*, LoadBalancer_*, LoadMonitor_* X-Git-Tag: 1.31.0-rc.0~17508 X-Git-Url: http://git.cyclocoop.org/%7D%7Cconcat%7B?a=commitdiff_plain;h=210edff11b37ebb5b1439a2a1eb454cbac21565e;p=lhc%2Fweb%2Fwiklou.git Remove underscore from classes LBFactory_*, LoadBalancer_*, LoadMonitor_* Part of program to remove underscores from class names. Checked core and 600+ extensions for occurrences. All uses are in core in core are updated in this patch. Patches for extensions: * https://gerrit.wikimedia.org/r/#/c/96463/ CentralAuth * https://gerrit.wikimedia.org/r/#/c/96460/ SimpleSecurity * https://gerrit.wikimedia.org/r/#/c/96459/ WikiBase * https://gerrit.wikimedia.org/r/#/c/96472/ Wikimedia configuration Antoine added tests to test the back compatibility. Change-Id: I507ba00a83bca8375a0215f00891b47e3c7afb9f --- diff --git a/RELEASE-NOTES-1.23 b/RELEASE-NOTES-1.23 index db80ad3c09..d9fbcbaae6 100644 --- a/RELEASE-NOTES-1.23 +++ b/RELEASE-NOTES-1.23 @@ -24,6 +24,8 @@ production. * Preferences 'watchcreations' and 'watchdefault' ("Add pages I create and files I upload to my watchlist", "Add pages and files I edit to my watchlist") are now enabled by default. +* $wgLBFactoryConf: Class names have had underscores removed. The configuration + should be updated if LBFactory_Simple or LBFactory_Multi is configured. === New features in 1.23 === * ResourceLoader can utilize the Web Storage API to cache modules client-side. @@ -90,6 +92,14 @@ production. properly for all parameters. * ApiQueryBase::titlePartToKey allows an extra parameter that indicates the namespace in order to properly capitalize the title part. +* Renamed classes: + - LBFactory_Fake to LBFactoryFake + - LBFactory_Multi to LBFactoryMulti + - LBFactory_Simple to LBFactorySimple + - LBFactory_Single to LBFactorySingle + - LoadBalancer_Single to LoadBalancerSingle + - LoadMonitor_MySQL to LoadMonitorMySQL + - LoadMonitor_Null to LoadMonitorNull === Languages updated in 1.23 === diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 9a9ce17e61..583f8896ea 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -461,16 +461,16 @@ $wgAutoloadLocalClasses = array( 'FakeResultWrapper' => 'includes/db/DatabaseUtility.php', 'Field' => 'includes/db/DatabaseUtility.php', 'LBFactory' => 'includes/db/LBFactory.php', - 'LBFactory_Fake' => 'includes/db/LBFactory.php', - 'LBFactory_Multi' => 'includes/db/LBFactory_Multi.php', - 'LBFactory_Simple' => 'includes/db/LBFactory.php', - 'LBFactory_Single' => 'includes/db/LBFactory_Single.php', + 'LBFactoryFake' => 'includes/db/LBFactory.php', + 'LBFactoryMulti' => 'includes/db/LBFactoryMulti.php', + 'LBFactorySimple' => 'includes/db/LBFactory.php', + 'LBFactorySingle' => 'includes/db/LBFactorySingle.php', 'LikeMatch' => 'includes/db/DatabaseUtility.php', 'LoadBalancer' => 'includes/db/LoadBalancer.php', - 'LoadBalancer_Single' => 'includes/db/LBFactory_Single.php', + 'LoadBalancerSingle' => 'includes/db/LBFactorySingle.php', 'LoadMonitor' => 'includes/db/LoadMonitor.php', - 'LoadMonitor_MySQL' => 'includes/db/LoadMonitor.php', - 'LoadMonitor_Null' => 'includes/db/LoadMonitor.php', + 'LoadMonitorMySQL' => 'includes/db/LoadMonitor.php', + 'LoadMonitorNull' => 'includes/db/LoadMonitor.php', 'MssqlField' => 'includes/db/DatabaseMssql.php', 'MssqlResult' => 'includes/db/DatabaseMssql.php', 'MySQLField' => 'includes/db/DatabaseMysqlBase.php', diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 0f7707c380..f799452653 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -1676,10 +1676,10 @@ $wgDBservers = false; * The class identified here is responsible for reading $wgDBservers, * $wgDBserver, etc., so overriding it may cause those globals to be ignored. * - * The LBFactory_Multi class is provided for this purpose, please see - * includes/db/LBFactory_Multi.php for configuration information. + * The LBFactoryMulti class is provided for this purpose, please see + * includes/db/LBFactoryMulti.php for configuration information. */ -$wgLBFactoryConf = array( 'class' => 'LBFactory_Simple' ); +$wgLBFactoryConf = array( 'class' => 'LBFactorySimple' ); /** * How long to wait for a slave to catch up to the master @@ -1828,7 +1828,7 @@ $wgExternalStores = array(); * ); * @endcode * - * Used by LBFactory_Simple, may be ignored if $wgLBFactoryConf is set to + * Used by LBFactorySimple, may be ignored if $wgLBFactoryConf is set to * another class. */ $wgExternalServers = array(); diff --git a/includes/dao/DBAccessBase.php b/includes/dao/DBAccessBase.php index a9f7b7f39e..2a0fde37ca 100644 --- a/includes/dao/DBAccessBase.php +++ b/includes/dao/DBAccessBase.php @@ -2,7 +2,7 @@ /** * Base class for objects that allow access to other wiki's databases using - * the foreign database access mechanism implemented by LBFactory_multi. + * the foreign database access mechanism implemented by LBFactoryMulti. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/includes/db/LBFactory.php b/includes/db/LBFactory.php index 6e377ff7be..c4e7976e04 100644 --- a/includes/db/LBFactory.php +++ b/includes/db/LBFactory.php @@ -37,7 +37,7 @@ abstract class LBFactory { */ public static function disableBackend() { global $wgLBFactoryConf; - self::$instance = new LBFactory_Fake( $wgLBFactoryConf ); + self::$instance = new LBFactoryFake( $wgLBFactoryConf ); } /** @@ -47,14 +47,44 @@ abstract class LBFactory { */ static function &singleton() { if ( is_null( self::$instance ) ) { - global $wgLBFactoryConf; - $class = $wgLBFactoryConf['class']; - self::$instance = new $class( $wgLBFactoryConf ); + $LBFactoryConf = self::getLBFactoryClass(); + + self::$instance = new $LBFactoryConf[0]( $LBFactoryConf[1] ); } return self::$instance; } + /** + * Returns the LBFactory class to use and the load balancer configuration. + * + * @return array ( factory class, $wgLBFactoryConf ) + */ + static function getLBFactoryClass() { + global $wgLBFactoryConf; + + // For configuration backward compatibility after removing + // underscores from class names in MediaWiki 1.23. + $bcClasses = array( + 'LBFactory_Simple' => 'LBFactorySimple', + 'LBFactory_Single' => 'LBFactorySingle', + 'LBFactory_Multi' => 'LBFactoryMulti', + 'LBFactory_Fake' => 'LBFactoryFake', + ); + + $class = $wgLBFactoryConf['class']; + + if ( in_array( $class, array_keys( $bcClasses ) ) ) { + $class = $bcClasses[$class]; + wfDeprecated( + '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details', + '1.23' + ); + } + + return array( $class, $wgLBFactoryConf ); + } + /** * Shut down, close connections and destroy the cached instance. */ @@ -167,7 +197,7 @@ abstract class LBFactory { /** * A simple single-master LBFactory that gets its configuration from the b/c globals */ -class LBFactory_Simple extends LBFactory { +class LBFactorySimple extends LBFactory { /** * @var LoadBalancer @@ -299,7 +329,7 @@ class LBFactory_Simple extends LBFactory { * Call LBFactory::disableBackend() to start using this, and * LBFactory::enableBackend() to return to normal behavior */ -class LBFactory_Fake extends LBFactory { +class LBFactoryFake extends LBFactory { function __construct( $conf ) { } diff --git a/includes/db/LBFactoryMulti.php b/includes/db/LBFactoryMulti.php new file mode 100644 index 0000000000..2f4cb57bd9 --- /dev/null +++ b/includes/db/LBFactoryMulti.php @@ -0,0 +1,337 @@ + array( + * 'db1' => 100, + * 'db2' => 100 + * ) + * ) + * + * serverTemplate A server info associative array as documented for $wgDBservers. + * The host, hostName and load entries will be overridden. + * + * groupLoadsBySection A 3-d map giving server load ratios for each section and group. + * For example: + * array( + * 'section1' => array( + * 'group1' => array( + * 'db1' => 100, + * 'db2' => 100 + * ) + * ) + * ) + * + * groupLoadsByDB A 3-d map giving server load ratios by DB name. + * + * hostsByName A map of hostname to IP address. + * + * externalLoads A map of external storage cluster name to server load map + * + * externalTemplateOverrides A set of server info keys overriding serverTemplate for external + * storage + * + * templateOverridesByServer A 2-d map overriding serverTemplate and + * externalTemplateOverrides on a server-by-server basis. Applies + * to both core and external storage. + * + * templateOverridesByCluster A 2-d map overriding the server info by external storage cluster + * + * masterTemplateOverrides An override array for all master servers. + * + * readOnlyBySection A map of section name to read-only message. + * Missing or false for read/write. + * + * @ingroup Database + */ +class LBFactoryMulti extends LBFactory { + // Required settings + var $sectionsByDB, $sectionLoads, $serverTemplate; + // Optional settings + var $groupLoadsBySection = array(), $groupLoadsByDB = array(), $hostsByName = array(); + var $externalLoads = array(), $externalTemplateOverrides, $templateOverridesByServer; + var $templateOverridesByCluster, $masterTemplateOverrides, $readOnlyBySection = array(); + // Other stuff + var $conf, $mainLBs = array(), $extLBs = array(); + var $lastWiki, $lastSection; + + /** + * @param $conf array + * @throws MWException + */ + function __construct( $conf ) { + $this->chronProt = new ChronologyProtector; + $this->conf = $conf; + $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' ); + $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName', + 'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer', + 'templateOverridesByCluster', 'masterTemplateOverrides', + 'readOnlyBySection' ); + + foreach ( $required as $key ) { + if ( !isset( $conf[$key] ) ) { + throw new MWException( __CLASS__ . ": $key is required in configuration" ); + } + $this->$key = $conf[$key]; + } + + foreach ( $optional as $key ) { + if ( isset( $conf[$key] ) ) { + $this->$key = $conf[$key]; + } + } + + // Check for read-only mode + $section = $this->getSectionForWiki(); + if ( !empty( $this->readOnlyBySection[$section] ) ) { + global $wgReadOnly; + $wgReadOnly = $this->readOnlyBySection[$section]; + } + } + + /** + * @param $wiki bool|string + * @return string + */ + function getSectionForWiki( $wiki = false ) { + if ( $this->lastWiki === $wiki ) { + return $this->lastSection; + } + list( $dbName, ) = $this->getDBNameAndPrefix( $wiki ); + if ( isset( $this->sectionsByDB[$dbName] ) ) { + $section = $this->sectionsByDB[$dbName]; + } else { + $section = 'DEFAULT'; + } + $this->lastSection = $section; + $this->lastWiki = $wiki; + + return $section; + } + + /** + * @param $wiki bool|string + * @return LoadBalancer + */ + function newMainLB( $wiki = false ) { + list( $dbName, ) = $this->getDBNameAndPrefix( $wiki ); + $section = $this->getSectionForWiki( $wiki ); + $groupLoads = array(); + if ( isset( $this->groupLoadsByDB[$dbName] ) ) { + $groupLoads = $this->groupLoadsByDB[$dbName]; + } + + if ( isset( $this->groupLoadsBySection[$section] ) ) { + $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] ); + } + + return $this->newLoadBalancer( + $this->serverTemplate, + $this->sectionLoads[$section], + $groupLoads + ); + } + + /** + * @param $wiki bool|string + * @return LoadBalancer + */ + function getMainLB( $wiki = false ) { + $section = $this->getSectionForWiki( $wiki ); + if ( !isset( $this->mainLBs[$section] ) ) { + $lb = $this->newMainLB( $wiki, $section ); + $lb->parentInfo( array( 'id' => "main-$section" ) ); + $this->chronProt->initLB( $lb ); + $this->mainLBs[$section] = $lb; + } + + return $this->mainLBs[$section]; + } + + /** + * @param string $cluster + * @param bool $wiki + * @throws MWException + * @return LoadBalancer + */ + function newExternalLB( $cluster, $wiki = false ) { + if ( !isset( $this->externalLoads[$cluster] ) ) { + throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" ); + } + $template = $this->serverTemplate; + if ( isset( $this->externalTemplateOverrides ) ) { + $template = $this->externalTemplateOverrides + $template; + } + if ( isset( $this->templateOverridesByCluster[$cluster] ) ) { + $template = $this->templateOverridesByCluster[$cluster] + $template; + } + + return $this->newLoadBalancer( $template, $this->externalLoads[$cluster], array() ); + } + + /** + * @param $cluster + * @param $wiki + * @return LoadBalancer + */ + function &getExternalLB( $cluster, $wiki = false ) { + if ( !isset( $this->extLBs[$cluster] ) ) { + $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki ); + $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) ); + $this->chronProt->initLB( $this->extLBs[$cluster] ); + } + + return $this->extLBs[$cluster]; + } + + /** + * Make a new load balancer object based on template and load array + * + * @param $template + * @param $loads array + * @param $groupLoads + * @return LoadBalancer + */ + function newLoadBalancer( $template, $loads, $groupLoads ) { + global $wgMasterWaitTimeout; + $servers = $this->makeServerArray( $template, $loads, $groupLoads ); + $lb = new LoadBalancer( array( + 'servers' => $servers, + 'masterWaitTimeout' => $wgMasterWaitTimeout + ) ); + + return $lb; + } + + /** + * Make a server array as expected by LoadBalancer::__construct, using a template and load array + * + * @param $template + * @param $loads array + * @param $groupLoads + * @return array + */ + function makeServerArray( $template, $loads, $groupLoads ) { + $servers = array(); + $master = true; + $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads ); + foreach ( $groupLoadsByServer as $server => $stuff ) { + if ( !isset( $loads[$server] ) ) { + $loads[$server] = 0; + } + } + foreach ( $loads as $serverName => $load ) { + $serverInfo = $template; + if ( $master ) { + $serverInfo['master'] = true; + if ( isset( $this->masterTemplateOverrides ) ) { + $serverInfo = $this->masterTemplateOverrides + $serverInfo; + } + $master = false; + } + if ( isset( $this->templateOverridesByServer[$serverName] ) ) { + $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo; + } + if ( isset( $groupLoadsByServer[$serverName] ) ) { + $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName]; + } + if ( isset( $this->hostsByName[$serverName] ) ) { + $serverInfo['host'] = $this->hostsByName[$serverName]; + } else { + $serverInfo['host'] = $serverName; + } + $serverInfo['hostName'] = $serverName; + $serverInfo['load'] = $load; + $servers[] = $serverInfo; + } + + return $servers; + } + + /** + * Take a group load array indexed by group then server, and reindex it by server then group + * @param $groupLoads + * @return array + */ + function reindexGroupLoads( $groupLoads ) { + $reindexed = array(); + foreach ( $groupLoads as $group => $loads ) { + foreach ( $loads as $server => $load ) { + $reindexed[$server][$group] = $load; + } + } + + return $reindexed; + } + + /** + * Get the database name and prefix based on the wiki ID + * @param $wiki bool + * @return array + */ + function getDBNameAndPrefix( $wiki = false ) { + if ( $wiki === false ) { + global $wgDBname, $wgDBprefix; + + return array( $wgDBname, $wgDBprefix ); + } else { + return wfSplitWikiID( $wiki ); + } + } + + /** + * Execute a function for each tracked load balancer + * The callback is called with the load balancer as the first parameter, + * and $params passed as the subsequent parameters. + * @param $callback + * @param $params array + */ + function forEachLB( $callback, $params = array() ) { + foreach ( $this->mainLBs as $lb ) { + call_user_func_array( $callback, array_merge( array( $lb ), $params ) ); + } + foreach ( $this->extLBs as $lb ) { + call_user_func_array( $callback, array_merge( array( $lb ), $params ) ); + } + } + + function shutdown() { + foreach ( $this->mainLBs as $lb ) { + $this->chronProt->shutdownLB( $lb ); + } + foreach ( $this->extLBs as $extLB ) { + $this->chronProt->shutdownLB( $extLB ); + } + $this->chronProt->shutdown(); + $this->commitMasterChanges(); + } +} diff --git a/includes/db/LBFactorySingle.php b/includes/db/LBFactorySingle.php new file mode 100644 index 0000000000..fa9959f7bd --- /dev/null +++ b/includes/db/LBFactorySingle.php @@ -0,0 +1,118 @@ +lb = new LoadBalancerSingle( $conf ); + } + + /** + * @param $wiki bool|string + * + * @return LoadBalancerSingle + */ + function newMainLB( $wiki = false ) { + return $this->lb; + } + + /** + * @param $wiki bool|string + * + * @return LoadBalancerSingle + */ + function getMainLB( $wiki = false ) { + return $this->lb; + } + + /** + * @param $cluster + * @param $wiki bool|string + * + * @return LoadBalancerSingle + */ + function newExternalLB( $cluster, $wiki = false ) { + return $this->lb; + } + + /** + * @param $cluster + * @param $wiki bool|string + * + * @return LoadBalancerSingle + */ + function &getExternalLB( $cluster, $wiki = false ) { + return $this->lb; + } + + /** + * @param $callback string|array + * @param $params array + */ + function forEachLB( $callback, $params = array() ) { + call_user_func_array( $callback, array_merge( array( $this->lb ), $params ) ); + } +} + +/** + * Helper class for LBFactorySingle. + */ +class LoadBalancerSingle extends LoadBalancer { + + /** + * @var DatabaseBase + */ + var $db; + + /** + * @param $params array + */ + function __construct( $params ) { + $this->db = $params['connection']; + parent::__construct( array( 'servers' => array( array( + 'type' => $this->db->getType(), + 'host' => $this->db->getServer(), + 'dbname' => $this->db->getDBname(), + 'load' => 1, + ) ) ) ); + } + + /** + * + * @param $server string + * @param $dbNameOverride bool + * + * @return DatabaseBase + */ + function reallyOpenConnection( $server, $dbNameOverride = false ) { + return $this->db; + } +} diff --git a/includes/db/LBFactory_Multi.php b/includes/db/LBFactory_Multi.php deleted file mode 100644 index a37a560505..0000000000 --- a/includes/db/LBFactory_Multi.php +++ /dev/null @@ -1,337 +0,0 @@ - array( - * 'db1' => 100, - * 'db2' => 100 - * ) - * ) - * - * serverTemplate A server info associative array as documented for $wgDBservers. - * The host, hostName and load entries will be overridden. - * - * groupLoadsBySection A 3-d map giving server load ratios for each section and group. - * For example: - * array( - * 'section1' => array( - * 'group1' => array( - * 'db1' => 100, - * 'db2' => 100 - * ) - * ) - * ) - * - * groupLoadsByDB A 3-d map giving server load ratios by DB name. - * - * hostsByName A map of hostname to IP address. - * - * externalLoads A map of external storage cluster name to server load map - * - * externalTemplateOverrides A set of server info keys overriding serverTemplate for external - * storage - * - * templateOverridesByServer A 2-d map overriding serverTemplate and - * externalTemplateOverrides on a server-by-server basis. Applies - * to both core and external storage. - * - * templateOverridesByCluster A 2-d map overriding the server info by external storage cluster - * - * masterTemplateOverrides An override array for all master servers. - * - * readOnlyBySection A map of section name to read-only message. - * Missing or false for read/write. - * - * @ingroup Database - */ -class LBFactory_Multi extends LBFactory { - // Required settings - var $sectionsByDB, $sectionLoads, $serverTemplate; - // Optional settings - var $groupLoadsBySection = array(), $groupLoadsByDB = array(), $hostsByName = array(); - var $externalLoads = array(), $externalTemplateOverrides, $templateOverridesByServer; - var $templateOverridesByCluster, $masterTemplateOverrides, $readOnlyBySection = array(); - // Other stuff - var $conf, $mainLBs = array(), $extLBs = array(); - var $lastWiki, $lastSection; - - /** - * @param $conf array - * @throws MWException - */ - function __construct( $conf ) { - $this->chronProt = new ChronologyProtector; - $this->conf = $conf; - $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' ); - $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName', - 'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer', - 'templateOverridesByCluster', 'masterTemplateOverrides', - 'readOnlyBySection' ); - - foreach ( $required as $key ) { - if ( !isset( $conf[$key] ) ) { - throw new MWException( __CLASS__ . ": $key is required in configuration" ); - } - $this->$key = $conf[$key]; - } - - foreach ( $optional as $key ) { - if ( isset( $conf[$key] ) ) { - $this->$key = $conf[$key]; - } - } - - // Check for read-only mode - $section = $this->getSectionForWiki(); - if ( !empty( $this->readOnlyBySection[$section] ) ) { - global $wgReadOnly; - $wgReadOnly = $this->readOnlyBySection[$section]; - } - } - - /** - * @param $wiki bool|string - * @return string - */ - function getSectionForWiki( $wiki = false ) { - if ( $this->lastWiki === $wiki ) { - return $this->lastSection; - } - list( $dbName, ) = $this->getDBNameAndPrefix( $wiki ); - if ( isset( $this->sectionsByDB[$dbName] ) ) { - $section = $this->sectionsByDB[$dbName]; - } else { - $section = 'DEFAULT'; - } - $this->lastSection = $section; - $this->lastWiki = $wiki; - - return $section; - } - - /** - * @param $wiki bool|string - * @return LoadBalancer - */ - function newMainLB( $wiki = false ) { - list( $dbName, ) = $this->getDBNameAndPrefix( $wiki ); - $section = $this->getSectionForWiki( $wiki ); - $groupLoads = array(); - if ( isset( $this->groupLoadsByDB[$dbName] ) ) { - $groupLoads = $this->groupLoadsByDB[$dbName]; - } - - if ( isset( $this->groupLoadsBySection[$section] ) ) { - $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] ); - } - - return $this->newLoadBalancer( - $this->serverTemplate, - $this->sectionLoads[$section], - $groupLoads - ); - } - - /** - * @param $wiki bool|string - * @return LoadBalancer - */ - function getMainLB( $wiki = false ) { - $section = $this->getSectionForWiki( $wiki ); - if ( !isset( $this->mainLBs[$section] ) ) { - $lb = $this->newMainLB( $wiki, $section ); - $lb->parentInfo( array( 'id' => "main-$section" ) ); - $this->chronProt->initLB( $lb ); - $this->mainLBs[$section] = $lb; - } - - return $this->mainLBs[$section]; - } - - /** - * @param string $cluster - * @param bool $wiki - * @throws MWException - * @return LoadBalancer - */ - function newExternalLB( $cluster, $wiki = false ) { - if ( !isset( $this->externalLoads[$cluster] ) ) { - throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" ); - } - $template = $this->serverTemplate; - if ( isset( $this->externalTemplateOverrides ) ) { - $template = $this->externalTemplateOverrides + $template; - } - if ( isset( $this->templateOverridesByCluster[$cluster] ) ) { - $template = $this->templateOverridesByCluster[$cluster] + $template; - } - - return $this->newLoadBalancer( $template, $this->externalLoads[$cluster], array() ); - } - - /** - * @param $cluster - * @param $wiki - * @return LoadBalancer - */ - function &getExternalLB( $cluster, $wiki = false ) { - if ( !isset( $this->extLBs[$cluster] ) ) { - $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki ); - $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) ); - $this->chronProt->initLB( $this->extLBs[$cluster] ); - } - - return $this->extLBs[$cluster]; - } - - /** - * Make a new load balancer object based on template and load array - * - * @param $template - * @param $loads array - * @param $groupLoads - * @return LoadBalancer - */ - function newLoadBalancer( $template, $loads, $groupLoads ) { - global $wgMasterWaitTimeout; - $servers = $this->makeServerArray( $template, $loads, $groupLoads ); - $lb = new LoadBalancer( array( - 'servers' => $servers, - 'masterWaitTimeout' => $wgMasterWaitTimeout - ) ); - - return $lb; - } - - /** - * Make a server array as expected by LoadBalancer::__construct, using a template and load array - * - * @param $template - * @param $loads array - * @param $groupLoads - * @return array - */ - function makeServerArray( $template, $loads, $groupLoads ) { - $servers = array(); - $master = true; - $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads ); - foreach ( $groupLoadsByServer as $server => $stuff ) { - if ( !isset( $loads[$server] ) ) { - $loads[$server] = 0; - } - } - foreach ( $loads as $serverName => $load ) { - $serverInfo = $template; - if ( $master ) { - $serverInfo['master'] = true; - if ( isset( $this->masterTemplateOverrides ) ) { - $serverInfo = $this->masterTemplateOverrides + $serverInfo; - } - $master = false; - } - if ( isset( $this->templateOverridesByServer[$serverName] ) ) { - $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo; - } - if ( isset( $groupLoadsByServer[$serverName] ) ) { - $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName]; - } - if ( isset( $this->hostsByName[$serverName] ) ) { - $serverInfo['host'] = $this->hostsByName[$serverName]; - } else { - $serverInfo['host'] = $serverName; - } - $serverInfo['hostName'] = $serverName; - $serverInfo['load'] = $load; - $servers[] = $serverInfo; - } - - return $servers; - } - - /** - * Take a group load array indexed by group then server, and reindex it by server then group - * @param $groupLoads - * @return array - */ - function reindexGroupLoads( $groupLoads ) { - $reindexed = array(); - foreach ( $groupLoads as $group => $loads ) { - foreach ( $loads as $server => $load ) { - $reindexed[$server][$group] = $load; - } - } - - return $reindexed; - } - - /** - * Get the database name and prefix based on the wiki ID - * @param $wiki bool - * @return array - */ - function getDBNameAndPrefix( $wiki = false ) { - if ( $wiki === false ) { - global $wgDBname, $wgDBprefix; - - return array( $wgDBname, $wgDBprefix ); - } else { - return wfSplitWikiID( $wiki ); - } - } - - /** - * Execute a function for each tracked load balancer - * The callback is called with the load balancer as the first parameter, - * and $params passed as the subsequent parameters. - * @param $callback - * @param $params array - */ - function forEachLB( $callback, $params = array() ) { - foreach ( $this->mainLBs as $lb ) { - call_user_func_array( $callback, array_merge( array( $lb ), $params ) ); - } - foreach ( $this->extLBs as $lb ) { - call_user_func_array( $callback, array_merge( array( $lb ), $params ) ); - } - } - - function shutdown() { - foreach ( $this->mainLBs as $lb ) { - $this->chronProt->shutdownLB( $lb ); - } - foreach ( $this->extLBs as $extLB ) { - $this->chronProt->shutdownLB( $extLB ); - } - $this->chronProt->shutdown(); - $this->commitMasterChanges(); - } -} diff --git a/includes/db/LBFactory_Single.php b/includes/db/LBFactory_Single.php deleted file mode 100644 index 7dca06d7d7..0000000000 --- a/includes/db/LBFactory_Single.php +++ /dev/null @@ -1,118 +0,0 @@ -lb = new LoadBalancer_Single( $conf ); - } - - /** - * @param $wiki bool|string - * - * @return LoadBalancer_Single - */ - function newMainLB( $wiki = false ) { - return $this->lb; - } - - /** - * @param $wiki bool|string - * - * @return LoadBalancer_Single - */ - function getMainLB( $wiki = false ) { - return $this->lb; - } - - /** - * @param $cluster - * @param $wiki bool|string - * - * @return LoadBalancer_Single - */ - function newExternalLB( $cluster, $wiki = false ) { - return $this->lb; - } - - /** - * @param $cluster - * @param $wiki bool|string - * - * @return LoadBalancer_Single - */ - function &getExternalLB( $cluster, $wiki = false ) { - return $this->lb; - } - - /** - * @param $callback string|array - * @param $params array - */ - function forEachLB( $callback, $params = array() ) { - call_user_func_array( $callback, array_merge( array( $this->lb ), $params ) ); - } -} - -/** - * Helper class for LBFactory_Single. - */ -class LoadBalancer_Single extends LoadBalancer { - - /** - * @var DatabaseBase - */ - var $db; - - /** - * @param $params array - */ - function __construct( $params ) { - $this->db = $params['connection']; - parent::__construct( array( 'servers' => array( array( - 'type' => $this->db->getType(), - 'host' => $this->db->getServer(), - 'dbname' => $this->db->getDBname(), - 'load' => 1, - ) ) ) ); - } - - /** - * - * @param $server string - * @param $dbNameOverride bool - * - * @return DatabaseBase - */ - function reallyOpenConnection( $server, $dbNameOverride = false ) { - return $this->db; - } -} diff --git a/includes/db/LoadBalancer.php b/includes/db/LoadBalancer.php index 9967cc0f61..7dcb88f15e 100644 --- a/includes/db/LoadBalancer.php +++ b/includes/db/LoadBalancer.php @@ -72,9 +72,9 @@ class LoadBalancer { } else { $master = reset( $params['servers'] ); if ( isset( $master['type'] ) && $master['type'] === 'mysql' ) { - $this->mLoadMonitorClass = 'LoadMonitor_MySQL'; + $this->mLoadMonitorClass = 'LoadMonitorMySQL'; } else { - $this->mLoadMonitorClass = 'LoadMonitor_Null'; + $this->mLoadMonitorClass = 'LoadMonitorNull'; } } diff --git a/includes/db/LoadMonitor.php b/includes/db/LoadMonitor.php index b2935316ed..58544565f3 100644 --- a/includes/db/LoadMonitor.php +++ b/includes/db/LoadMonitor.php @@ -71,11 +71,7 @@ interface LoadMonitor { function getLagTimes( $serverIndexes, $wiki ); } -/** - * @todo FIXME: Should be LoadMonitorNull per naming conventions. - * PHP CodeSniffer Squiz.Classes.ValidClassName.NotCamelCaps - */ -class LoadMonitor_Null implements LoadMonitor { +class LoadMonitorNull implements LoadMonitor { function __construct( $parent ) { } @@ -100,10 +96,8 @@ class LoadMonitor_Null implements LoadMonitor { * Uses memcached to cache the replication lag for a short time * * @ingroup Database - * @todo FIXME: Should be LoadMonitorMySQL per naming conventions. - * PHP CodeSniffer Squiz.Classes.ValidClassName.NotCamelCaps */ -class LoadMonitor_MySQL implements LoadMonitor { +class LoadMonitorMySQL implements LoadMonitor { /** * @var LoadBalancer */ diff --git a/includes/installer/DatabaseInstaller.php b/includes/installer/DatabaseInstaller.php index b4f21943ee..321884bd55 100644 --- a/includes/installer/DatabaseInstaller.php +++ b/includes/installer/DatabaseInstaller.php @@ -260,7 +260,7 @@ abstract class DatabaseInstaller { if ( !$status->isOK() ) { throw new MWException( __METHOD__ . ': unexpected DB connection error' ); } - LBFactory::setInstance( new LBFactory_Single( array( + LBFactory::setInstance( new LBFactorySingle( array( 'connection' => $status->value ) ) ); } diff --git a/tests/phpunit/includes/db/LBFactoryTest.php b/tests/phpunit/includes/db/LBFactoryTest.php new file mode 100644 index 0000000000..f64a784d4d --- /dev/null +++ b/tests/phpunit/includes/db/LBFactoryTest.php @@ -0,0 +1,82 @@ +getMockBuilder( 'DatabaseMysql' ) + -> disableOriginalConstructor() + ->getMock(); + $this->setMwGlobals( 'wgLBFactoryConf', + array( + 'class' => $deprecated, + 'connection' => $mockDB, + # Various other parameters required: + 'sectionsByDB' => array(), + 'sectionLoads' => array(), + 'serverTemplate' => array(), + ) + ); + + global $wgLBFactoryConf; + $this->assertArrayHasKey( 'class', $wgLBFactoryConf ); + $this->assertEquals( $wgLBFactoryConf['class'], $deprecated ); + + # The point of this test is to call a deprecated interface and make + # sure it keeps back compatibility, so skip the deprecation warning. + $this->hideDeprecated( '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details' ); + $lbfactory = FakeLBFactory::singleton(); + $this->assertInstanceOf( $expected, $lbfactory, + "LBFactory passed $deprecated should yield the new class $expected" ); + } + + function provideDeprecatedLbfactoryClasses() { + return array( + # Format: new class, old class + array( 'LBFactorySimple', 'LBFactory_Simple' ), + array( 'LBFactorySingle', 'LBFactory_Single' ), + array( 'LBFactoryMulti', 'LBFactory_Multi' ), + array( 'LBFactoryFake', 'LBFactory_Fake' ), + ); + } +}